<template>
<div class="main-panel">
  <div class="main-header">
    <div class="header-btn">
      <el-button type="primary" icon="el-icon-plus" @click="openDiaolog(null)">添加</el-button>
    </div>
  </div>
  <el-table
    v-loading="listLoading"
    :data="listData"
    row-key="id"
    :border="true"
    style="width: 100%"
    :tree-props="{children: 'children'}">
    <el-table-column
      prop="name"
      label="资源名称"
      width="140"
      header-align="center">
      <template slot-scope="scope">
        <i :class="scope.row.iconCls"></i>{{scope.row.name}}
      </template>
    </el-table-column>
    <el-table-column
      prop="url"
      label="资源路径">
    </el-table-column>
    <el-table-column prop="seq" label="排序" align="center" width="60">
    </el-table-column>
    <el-table-column prop="iconCls" label="图标" width=200 header-align="center"></el-table-column>
    <el-table-column prop="resourceType" label="资源类型" width=80 align="center">
      <template slot-scope="scope">
        <el-tag :type="scope.row.resourceType===0 ? 'info' : 'warning'">{{scope.row.resourceType===0?'菜单':'按钮'}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="status" label="状态" width=80 align="center">
      <template slot-scope="scope">
        <el-tag :type="scope.row.status===0 ? '' : 'danger'">{{scope.row.status===0?'正常':'停用'}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="action" label="操作" width=150 align="center">
      <template slot-scope="scope">
        <el-button size="mini" @click="openDiaolog(scope.row)">编辑</el-button>
        <el-button size="mini" type="danger" @click="deleteResource(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog
    :title="form.id?'修改':'添加'"
    :visible.sync="model"
    width="500px">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="资源名称" required>
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="资源类型">
        <el-radio-group v-model="form.resourceType">
          <el-radio-button :label="0">菜单</el-radio-button>
          <el-radio-button :label="1">按钮</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单图标">
        <el-input v-model="form.iconCls" @focus="iconsModel=true"></el-input>
      </el-form-item>
      <el-form-item label="资源路径">
        <el-input v-model="form.url"></el-input>
      </el-form-item>
      <el-form-item label="第三方链接">
        <el-input v-model="form.thirdUrl"></el-input>
      </el-form-item>
      <el-form-item label="组件" v-if="form.resourceType===0">
        <el-input v-model="form.component"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="form.seq" controls-position="right"></el-input-number>
      </el-form-item>
      <el-form-item label="状态">
        <el-tooltip :content="form.status===0?'正常':'禁用'" placement="right">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="0"
          :inactive-value="1">
        </el-switch>
        </el-tooltip>
      </el-form-item>
      <el-form-item label="上级资源">
        <el-select v-model="form.pid" filterable placeholder="请选择" value="请选择">
          <el-option
            v-for="item in pmenus"
            :key="item.id"
            :label="item.text"
            :value="item.id">
            <i :class="item.iconCls"></i>{{ item.text }}
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="model = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">{{form.id?'修 改':'保 存'}}</el-button>
    </span>
  </el-dialog>
  <icons v-model="iconsModel" :on-select="(selected)=>{form.iconCls=selected}"/>
</div>
</template>

<script>
import { getTreeGrid, getPMenus, add, edit, del } from '_api/resource'
import icons from '_c/diaolog/icons'

export default {
  name: 'manager',
  created () {
    this.loadData()
  },
  components: {
    icons
  },
  data () {
    return {
      listLoading: true,
      listData: [],
      model: false,
      pmenus: [],
      form: {
        name: '',
        resourceType: 0,
        status: 0,
        seq: 0,
        url: '',
        pid: undefined,
        component: '',
        iconCls: '',
        thirdUrl: ''
      },
      iconsModel: false
    }
  },
  methods: {
    loadData () {
      let _this = this
      _this.listLoading = true
      getTreeGrid().then(res => {
        _this.listData = res
        _this.listLoading = false
      })
    },
    resetForm () {
      this.form = {
        name: '',
        resourceType: 0,
        status: 0,
        seq: 0,
        url: '',
        pid: undefined,
        component: '',
        iconCls: '',
        thirdUrl: ''
      }
    },
    queryPmenus () {
      getPMenus().then(res => {
        this.pmenus = res
      })
    },
    openDiaolog (row) {
      this.queryPmenus()
      if (row) {
        this.form = {
          id: row.id,
          name: row.name,
          resourceType: row.resourceType,
          status: row.status,
          seq: row.seq,
          url: row.url,
          pid: row.pid,
          component: row.component,
          iconCls: row.iconCls,
          thirdUrl: row.thirdUrl
        }
      } else {
        this.resetForm()
      }
      this.model = true
    },
    handleSubmit () {
      this.model = false
      if (!this.form.id) {
        add(this.form).then((res) => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.loadData()
        })
      } else {
        edit(this.form).then((res) => {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.loadData()
        })
      }
    },
    deleteResource (id) {
      this.$confirm('您是否要删除当前资源？删除当前资源会连同子资源一起删除!', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del({ id }).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
